package com.example.administrator.zhongyichou.activity;

import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.Bean.ChoiceBean;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.presentor.BankTypeProtocol;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.view.ChoiceDialog;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/21.
 * 描述：补充大病资料第二步
 */

public class ReplenishmentDiseaseInformationTwoActivity extends BaseActivity implements ChoiceDialog.OnClik {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.ll_choose_prove)
    LinearLayout llChooseProve;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_id)
    EditText editId;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_bank_num)
    EditText editBankNum;
    @BindView(R.id.txt_bank)
    TextView txtBank;
    @BindView(R.id.radio_01)
    RadioButton radio01;
    @BindView(R.id.radio_02)
    RadioButton radio02;
    @BindView(R.id.radio_03)
    RadioButton radio03;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    private ChoiceDialog choiceDialog;
    private String infoID;
    private String bankType = "-1";
    private int relationshipType = 0;
    private BankTypeProtocol bankTypeProtocol;
    private String supply_id;
    private int intentType = 0;//0普通提交，1编辑

    @Override
    protected void init() {
        infoID = getIntent().getStringExtra("info_id");
        supply_id = getIntent().getStringExtra("supply_id");
        intentType = getIntent().getIntExtra("intentType", 0);
        ButterKnife.bind(this);
        initHead();
        initRadio();
        choiceDialog = new ChoiceDialog(ReplenishmentDiseaseInformationTwoActivity.this);
        choiceDialog.setOnClik(this);
    }

    private void initRadio() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.radio_01://患者本人
                        relationshipType = 0;
                        break;
                    case R.id.radio_02://直系亲属
                        relationshipType = 1;
                        break;
                    case R.id.radio_03://患者配偶
                        relationshipType = 2;
                        break;
                }
            }
        });
        radio01.setChecked(true);
    }

    @Override
    protected void responseData(GsonBaseProtocol mGsonBaseProtocal) {

    }

    @Override
    protected HttpLoader initHttpLoader() {
        return null;
    }

    @Override
    protected int initContentView() {
        return R.layout.replenishment_disease_two_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    private void initHead() {
        headerview.setTitle("资料证明");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @OnClick({R.id.ll_choose_prove, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_choose_prove:
                getBank();
                break;
            case R.id.btn_commit:
                commit();
                break;
        }
    }

    private void commit() {
        String name = editName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.showToast("请填写真实姓名");
            return;
        }
        String id = editId.getText().toString().trim();
        if (TextUtils.isEmpty(id)) {
            ToastUtil.showToast("请填写身份证号码");
            return;
        }
        String phone = editPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showToast("请填写手机号码");
            return;
        }
        if (bankType.equals("-1")) {
            ToastUtil.showToast("请选择开户银行");
            return;
        }
        String bankNum = editBankNum.getText().toString().trim();
        if (TextUtils.isEmpty(bankNum)) {
            ToastUtil.showToast("请填写银行卡号");
            return;
        }
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(GsonBaseProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        map.put("info_id", infoID);
//        map.put("supply_id", supply_id);
        map.put("receive_type", relationshipType + "");
        map.put("receive_name", name);
        map.put("receive_num", id);
        map.put("receive_tel", phone);
        map.put("bank_id", bankType);
        map.put("bank_card", bankNum);
        requestHttpLoader.setParams(NetConfig.SUPPLY_BANK, map);
        requestHttpLoader.executor(new ResultListener() {
            @Override
            public void success(GsonBaseProtocol mGsonBaseProtocol) {
                EventBus.getDefault().post(new BaseEventBean<>(Constants.TYPE_2));
                finish();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    private void getBank() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(BankTypeProtocol.class);
        Map<String, String> map = new HashMap<>();
        requestHttpLoader.setParams(NetConfig.BANK_TYPE, map);
        requestHttpLoader.showDialog(this, true);
        requestHttpLoader.executor(new ResultListener<BankTypeProtocol>() {
            @Override
            public void success(BankTypeProtocol mGsonBaseProtocol) {
                ReplenishmentDiseaseInformationTwoActivity.this.bankTypeProtocol = mGsonBaseProtocol;
                List<ChoiceBean> list = new ArrayList<ChoiceBean>();
                for (BankTypeProtocol.DataBean bean : mGsonBaseProtocol.getData()) {
                    ChoiceBean choiceBean = new ChoiceBean();
                    choiceBean.name = bean.getBank_name();
                    list.add(choiceBean);
                }
                choiceDialog.setData(list);
                choiceDialog.show();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    @Override
    public void onClik(int position) {
        bankType = bankTypeProtocol.getData().get(position).getBank_id();
        txtBank.setText(bankTypeProtocol.getData().get(position).getBank_name());
    }
}
